package com.devops.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.devops.admin.service.DOSK8sDeploymentService;
import com.devops.admin.util.Assert;
import com.devops.common.annotation.DOSLog;
import com.devops.common.utils.Message;
import com.devops.constant.DevOpsConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author jxt
 * @Notes
 * @date 2024/1/3 10:02
 */
@RestController
@RequestMapping("/api/k8s")
@Api(value = "/api/k8s",tags = "k8s相关接口")
public class K8sController {
    @Resource
    DOSK8sDeploymentService dosk8sDeploymentService;

    @GetMapping(value = "/clusters/{clusterId}/deployments/{deploymentName}")
    @ApiOperation(value="获取deployments信息",notes = "获取deploy信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clusterId",value = "clusterId",required = true,dataType = "int",paramType = "path"),
            @ApiImplicitParam(name = "deploymentName",value = "deploymentName",required = true,dataType = "String",paramType = "path")
    })
    public Message getDeploymentInfo(@PathVariable("clusterId") Integer clusterId,
                              @PathVariable("deploymentName") String deploymentName) {
        Assert.isTrueMvc(StrUtil.isNotBlank(deploymentName),"deploymentName不能为空");
        return Message.ok(dosk8sDeploymentService.queryDeploymentInfo(clusterId, deploymentName));
    }

    @PostMapping(value = "/clusters/{clusterId}/{namespace}/{deploymentName}/restart")
    @ApiOperation(value="重启deployment",notes = "重启deployment")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clusterId",value = "clusterId",required = true,dataType = "int",paramType = "path"),
            @ApiImplicitParam(name = "namespace",value = "namespace",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "deploymentName",value = "deploymentName",required = true,dataType = "String",paramType = "path")
    })
    @DOSLog(description = "重启deployment")
    public Message getDeploymentInfo(@RequestAttribute(DevOpsConstant.API_USER_ID) String apiUserId, @PathVariable("clusterId") Integer clusterId,
                                     @PathVariable("namespace") String namespace,
                                     @PathVariable("deploymentName") String deploymentName) {
        Assert.isTrueMvc(StrUtil.isNotBlank(apiUserId), "无权限");
        Assert.isTrueMvc(StrUtil.isNotBlank(namespace),"namespace不能为空");
        Assert.isTrueMvc(StrUtil.isNotBlank(deploymentName),"deploymentName不能为空");
        dosk8sDeploymentService.restart(clusterId, namespace, deploymentName);
        return Message.ok();
    }
}
